package com.soft.service;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Money;
import com.soft.mapper.MoneyMapper;

@Service
public class MoneyService {
@Autowired
private MoneyMapper moneyMapper;
public List<Money> queryAll(){
List<Money> moneyList = moneyMapper.selectList(null);
return moneyList;
}
public List<Money> queryByIncome(BigDecimal incomecount){
	QueryWrapper<Money> qw = new QueryWrapper<>();
	qw.eq("money_income",incomecount);
	List<Money> moneyList = moneyMapper.selectList(qw);
	return moneyList;
}
// 新增记录
public int addMoney(Money money) {
    if (money.getTime() == null) {
        money.setTime(LocalTime.now());
    }
    return moneyMapper.insert(money);
}

// 根据ID删除
public int deleteMoneyById(Long id) {
    return moneyMapper.deleteById(id);
}

// 根据条件删除
public int deleteMoneyByCondition(BigDecimal total) {
    QueryWrapper<Money> qw = new QueryWrapper<>();
    qw.le("money_total", total); // 删除总额小于等于指定值的记录
    return moneyMapper.delete(qw);
}

// 根据ID更新
public int updateMoneyById(Money money) {
    return moneyMapper.updateById(money);
}

// 条件更新（更新指定收入的记录的总额）
public int updateMoneyByIncome(BigDecimal income, BigDecimal newTotal) {
    UpdateWrapper<Money> uw = new UpdateWrapper<>();
    uw.eq("money_income", income);
    Money money = new Money();
    money.setTotal(newTotal);
    return moneyMapper.update(money, uw);
}
}

